package com.crowntit.domain.activity.service.discount.impl;

import com.crowntit.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.crowntit.domain.activity.service.discount.AbstractDiscountCalculateService;
import com.crowntit.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service("MJ")
public class MJCalculateService extends AbstractDiscountCalculateService {
    @Override
    public BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        log.info("优惠策略折扣计算:{}", groupBuyDiscount.getDiscountType().getCode());

        String discountMarketExpr = groupBuyDiscount.getMarketExpr();
        String[] splitExpr = discountMarketExpr.split(Constants.SPLIT);
        BigDecimal targetPrice = new BigDecimal(splitExpr[0]);
        BigDecimal subtractPrice = new BigDecimal(splitExpr[1]);

        if (originalPrice.compareTo(targetPrice) >= 0){
            return originalPrice.subtract(subtractPrice);
        }
        return originalPrice;
    }
}
